﻿# based on mongol_invasion_war

event_genpei_war = {
	icon = invasion
	group = event

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	allow_hostages = no
	allowed_for_character = {}

	allowed_against_character = {}

	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder
	
	valid_to_start = {
		scope:target = {
			tier = tier_empire
		}
	}

	should_invalidate = {}

	on_invalidated_desc = msg_mongol_invasion_war_invalidated_message
	
	on_invalidated = {}

	cost = {}
	
	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = shogunate_genpei_war_victory_desc_attacker
			}
			desc = shogunate_genpei_war_victory_desc
		}
		desc = shogunate_genpei_war_warning_desc
	}
	
	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		custom_tooltip = {
			text = shogunate_genpei_war_custom_tooltip_1
			if = {
				limit = {
					scope:defender = {
						any_character_war = {
							using_cb = event_genpei_war
							casus_belli = {
								NOT = { primary_attacker = scope:attacker }
							}
						}
					}
				}
				scope:defender = {
					every_character_war = {
						limit = {
							using_cb = event_genpei_war
							casus_belli = {
								NOT = { primary_attacker = scope:attacker }
							}
						}
						casus_belli = {
							primary_attacker = { save_scope_as = new_attacker }
						}
						every_war_attacker = {
							add_to_list = new_attacker_ally
						}
						scope:new_attacker = {
							start_war = {
								cb = event_genpei_war
								target = scope:attacker
								target_title = title:e_nmih_japan
							}
							every_character_war = {
								limit = {
									using_cb = event_genpei_war
									casus_belli = {
										primary_defender = scope:attacker
									}
								}
								save_scope_as = new_war
								every_in_list = {
									list = new_attacker_ally
									if = {
										limit = {
											NOT = { is_participant_in_war = scope:new_war }
										}
										save_scope_as = new_ally
										scope:new_war = {
											add_attacker = scope:new_ally
										}
									}
									remove_from_list = new_attacker_ally
								}
							}
						}
					}
				}
			}
		}

		custom_tooltip = {
			text = shogunate_genpei_war_custom_tooltip_2
			every_in_list = {
				list = target_titles
				every_in_de_jure_hierarchy = {
					continue = {
						OR = {
							NOT = {
								exists = holder
							}
							NOT = {
								holder = {
									target_is_liege_or_above = scope:defender
								}
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
						tier > tier_county
					}
					limit = {
						exists = holder
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}

					if = {
						limit = {
							holder = {
								target_is_liege_or_above = scope:defender
								NOT = { dynasty = scope:defender.dynasty }
							}
						}
						holder = {
							add_to_temporary_list = vassals_taken
						}
					}
					else = {
						add_to_temporary_list = titles_taken
					}
				}
			}

			every_in_list = {
				list = titles_taken
				change_title_holder = {
					holder = scope:attacker
					change = scope:change
					take_baronies = yes
				}
			}

			every_in_list = {
				list = vassals_taken
				change_liege = {
					liege = scope:attacker
					change = scope:change
				}
			}	

			hidden_effect = { resolve_title_and_vassal_change = scope:change }
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		
		# Prestige level progress for the attacker
		scope:attacker = {
			add_prestige_experience = {
				value = mongol_invasion_cb_prestige_experience_gain
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_prestige = mongol_invasion_cb_prestige_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = invasion_war_white_peace_desc_defender
			}
			desc = invasion_war_white_peace_desc
		}
	}
	
	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:defender = { accolade_defender_war_end_glory_gain_high_effect = yes }

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender # Not important as the scales are identical
			FAME_BASE = mongol_invasion_cb_ally_prestige
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
	
		# Truce
		add_truce_attacker_defeat_effect = yes
			
		# piety change
		every_in_list = {
			list = target_titles
			
			scope:defender = {
				add_prestige = mongol_invasion_cb_prestige_gain
			}

			scope:attacker = {
				if = {
					limit = {
						scope:defender = { is_ai = no }
					}
					if = {
						limit = {
							monthly_character_income > 0
						}
						pay_short_term_income = {
							years = 3
							target = scope:defender
						}
					}
					else = {
						pay_short_term_gold = {
							target = scope:defender
							gold = medium_gold_value
						}
					}
				}
			}
		}
		
		scope:attacker = {
			every_vassal = {
				custom = custom.every_vassal
				add_opinion = {
					modifier = liege_lost_mongol_invasion_war
					target = prev
				}
			}
		}
		
		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}
	
	transfer_behavior = transfer
	
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit
	
	attacker_allies_inherit = yes
	defender_allies_inherit = yes
	
	war_name = "EVENT_GENPEI_WAR_NAME"
	cb_name = "EVENT_GENPEI_WAR_CB_NAME"
	
	interface_priority = 200
	
	use_de_jure_wargoal_only = yes
	
	attacker_wargoal_percentage = 0.8
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	
	max_ai_diplo_distance_to_title = 500

	ai_score_mult = {
		value = 1
		#Declaring war on the frankokratia leader can really ruin the whole thing
		if = {
			limit = {
				scope:defender = {
					any_owned_story = {
						story_type = frankokratia_story
						NOT = { has_variable = franko_war_complete }
					}
				}
			}
			multiply = 0
		}
	}
}
